var key = 'shsytour.cn'
var sign  = require('sign.js')
var userOpCheck = require('userOperateCheck.js')
var app = getApp()
var token = '$token'
var user_sign = '$sign'

function login(data, cb) {
	if (userOpCheck.checkOperateCount('test','error')) {
		data.sign = sign.sign(data,key)
		app.request('wxapp/user/token', 'GET', {}, data, function(res){
			if (res.data.code == 200) {
				userOpCheck.clearOperateInfo('test','error')
				wx.setStorageSync(token, res.data.data.token)
				wx.setStorageSync(user_sign, res.data.data.user_info.sign)
				typeof cb == "function" && cb(res)
			}else{
				wx.showModal({
					title: '提示',
					showCancel: false,
					content: '登录失败..',
					success: function(res) {
					}
				})
			}
		})
	}else{
		wx.showModal({
			title: '提示',
			showCancel: false,
			content: '错误次数过多...',
			success: function(res) {
			}
		})
	}
}

function logout() {
	wx.removeStorageSync(token)
	app.globalData.userInfo = null
}

function checkLogin() {
	if (wx.getStorageSync(token)) {return true}
	return false
}

function getLoginUserInfo(cb) {
	app.getJson('/wxapp/user/user_info',{}, cb)
}

module.exports = {
	login: login,
	logout: logout,
	checkLogin: checkLogin,
	getLoginUserInfo: getLoginUserInfo
}
